import ipaddress

from PyQt5 import QtWidgets
from PyQt5.QtWidgets import QGridLayout

from configs.configdialog import ConfigurationDialog
from configs.strings import translate
from tools import tools

serial_nums = ['COM' + str(i) for i in range(1, 41)]
baud_rate = ['9600', '110', '300', '600', '1200', '2400', '4800',
             '14400', '19200', '38400', '56000', '57600', '115200',
             '128000', '256000']
parity_bit = ['NONE', 'ODD', 'EVEN', 'MARK', 'SPACE']
data_bit = ['8', '5', '6', '7']
stop_bit = ['1.0', '1.5', '2.0']


class UDPConfigDialog(ConfigurationDialog):
    def __init__(self, basic_config={}, lang='ZH'):
        super(UDPConfigDialog, self).__init__()
        self.lang = lang
        self.basic_config = basic_config
        self.init_ui()
        self.center()

    def set_attributes(self, basic_config, lang):
        self.lang = lang
        self.basic_config = basic_config

    def init_ui(self):
        self.setWindowTitle(translate["UDP_CONFIG"][self.lang])
        self.setGeometry(250, 300, 250, 300)
        self.setFixedSize(300, 300)
        self.glayout = QGridLayout()
        self.setLayout(self.glayout)

        self.ipAddrLabel = QtWidgets.QLabel(translate['IP_ADDR'][self.lang])
        self.ipAddrEdit = QtWidgets.QLineEdit(self.basic_config['IP_ADDR'])

        self.glayout.addWidget(self.ipAddrLabel, 0, 0)
        self.glayout.addWidget(self.ipAddrEdit, 0, 1)

        self.portLabel = QtWidgets.QLabel(translate['PORT'][self.lang])
        self.portEdit = QtWidgets.QLineEdit(str(self.basic_config['PORT']))
        self.glayout.addWidget(self.portLabel, 1, 0)
        self.glayout.addWidget(self.portEdit, 1, 1)

        self.ipHostAddrLabel = QtWidgets.QLabel(translate['HOST_ADDR'][self.lang])
        self.ipHostAddrEdit = QtWidgets.QLineEdit(self.basic_config['HOST_ADDR'])

        self.glayout.addWidget(self.ipHostAddrLabel, 2, 0)
        self.glayout.addWidget(self.ipHostAddrEdit, 2, 1)

        self.portHostLabel = QtWidgets.QLabel(translate['HOST_PORT'][self.lang])
        self.portHostEdit = QtWidgets.QLineEdit(str(self.basic_config['HOST_PORT']))
        self.glayout.addWidget(self.portHostLabel, 3, 0)
        self.glayout.addWidget(self.portHostEdit, 3, 1)

        self.buttons = QtWidgets.QDialogButtonBox(
            QtWidgets.QDialogButtonBox.Ok | QtWidgets.QDialogButtonBox.Cancel)  # 窗口中建立确认和取消按钮
        self.glayout.addWidget(self.buttons, 4, 1)

        self.buttons.accepted.connect(self.accept)
        self.buttons.rejected.connect(self.reject)

    def get_data(self):
        try:
            ip = str(ipaddress.ip_address(self.ipAddrEdit.text()))
            ipHost = str(ipaddress.ip_address(self.ipHostAddrEdit.text()))
            port = int(self.portEdit.text())
            portHost = str(self.portHostEdit.text())
            self.basic_config["IP_ADDR"] = ip
            self.basic_config["PORT"] = port
            self.basic_config["HOST_ADDR"] = ipHost
            self.basic_config['HOST_PORT'] = portHost
            return self.basic_config
        except Exception as e:
            tools.show_dialog(self, str(e) + " Configuration exception! Please retry!", tools.ERROR)
            return

